create procedure wp_Tagger
	@Tag varchar(max) = null,
	@DBName varchar(max) = null,
	@State int = null
as
	declare @DBID int
	declare @tempState int

	if (isnull(@DBName, '') = ''  and isnull(@State, 0) = 0)
	begin
		raiserror('DB name or State required!', 10, 1)
		return -1
	end
	if isnull(@DBName, '') <> ''
	begin
		select @DBID=db_id(@DBName)

		select @tempState = max(i.State) from Info i
			inner join Data d on i.State = d.State
			inner join Objects o on d.ObjectID = o.ID and o.DBID = @DBID
			where i.BranchID is null
		select @State = min(State) - 1 from Info
			where State > @tempState
		if isnull(@State, 0) = 0
			select @State = max(State) from Data
		if isnull(@State, 0) = 0
		begin
			raiserror('Table Data is empty', 10, 1)
			return -1
		end
	end

	if isnull(@Tag, '') = ''
		select @Tag = 'Unknown tag.'

	insert into Tag(
		State,
		Tag)
	select
		@State,
		@Tag

